package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.entity.User;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * Author：汤小洋
 * Date：2023-08-21 14:23
 * Description：<描述>
 */
@RestController
@RequiredArgsConstructor
public class RedisController {

    // private final StringRedisTemplate redisTemplate;
    private final RedisTemplate<String,Object> redisTemplate;

    @RequestMapping("/test1")
    public String test1(){
        // System.out.println(redisTemplate.getConnectionFactory().getConnection());

        /**
         * 操作String
         */
        // ValueOperations<String, String> ops = redisTemplate.opsForValue();
        // ops.set("name","tom");
        // ops.set("age","18", 3, TimeUnit.MINUTES);
        // Boolean flag = ops.setIfAbsent("sex", "男", 60, TimeUnit.SECONDS);
        // System.out.println(flag);

        // String name = redisTemplate.opsForValue().get("name");
        // System.out.println(name);

        User user = new User(1, "tom", "123456");
        redisTemplate.opsForValue().set("user",user);

        User u = (User) redisTemplate.opsForValue().get("user");
        System.out.println(u);

        return "test1";
    }

}
